package com.dao.impl;

import org.hibernate.query.Query;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.dao.UserDao;
import com.entity.User;

/**
 * @description:
 * @author:wsc
 * @date:2020年3月8日 下午4:02:17
 */
@Repository
public class UserDaoImpl implements UserDao{
	@Resource
	private SessionFactory seesionFactory;

	@Override
	public User login(String email, String password) {
		User u=null;
		String hql="from User where email=? and password=?";
		try{
			Query<?> query=this.seesionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, email);
			query.setParameter(1, password);
			u=(User) query.uniqueResult();
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		return u;
	}

	@Override
	public void addUser(User user) {
		try {
			this.seesionFactory.getCurrentSession().save(user);
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public void updateUser(User user) {
		try {
			this.seesionFactory.getCurrentSession().update(user);
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public void deleteUser(User user) {
		try {
			this.seesionFactory.getCurrentSession().delete(user);
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public User queryUser1(int uid) {
		String hql="from User where uid=:uid";
		Query<?> query=this.seesionFactory.getCurrentSession().createQuery(hql);
		User user=(User) query.setParameter("uid", uid).uniqueResult();
		return user;
	}

	@Override
	public User queryUser2(String email) {
		String hql="from User where email=?";
		Query<?> query=this.seesionFactory.getCurrentSession().createQuery(hql);
		User user=(User)query.uniqueResult();
		return user;
	}

	
}
